//
//  moreRemarkController.m
//  Focus
//
//  Created by dlios on 14-10-29.
//  Copyright (c) 2014年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "moreRemarkController.h"
#import "moreRemarkCell.h"
#import "AFNetworking.h"
#import "remarkModel.h"
#import "UIImageView+WebCache.h"
@interface moreRemarkController ()
@property (nonatomic, retain) UIView *myView;
@property (nonatomic, retain) NSString *str;
@end

@implementation moreRemarkController
-(void)dealloc{
    [_remarkArray release];
    [_table release];
    [_myDic release];
    [super dealloc];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.myDic = [NSMutableDictionary dictionary];
        self.remarkArray = [NSMutableArray array];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self creatView];
#pragma mark -
#pragma mark - 创建tableView
    [self creattable];
#pragma mark -
#pragma mark - 创建View
    UIView *downView = [[UIView alloc]initWithFrame:CGRectMake(0, 510, 320, 58)];
    downView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:downView];
    [downView release];
#pragma mark -
#pragma mark - 创建downView上面的button
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(10, 20, 30, 30);
    [button setBackgroundImage:[UIImage imageNamed:@"circle_icon_quit.png"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [downView addSubview:button];
    
#pragma mark -
#pragma mark - 注册cell
    [_table registerClass:[moreRemarkCell class] forCellReuseIdentifier:@"moreRemark"];
#pragma mark -
#pragma mark - 创建请求
    [self connect];
#pragma mark -
#pragma mark - 判断remarkArray数组为空时移除tableView创建view
    //    self.myView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 510)];
    //    if (self.remarkArray.count == 0) {
    //        self.myView.backgroundColor = [UIColor redColor];
    //        [self.table removeFromSuperview];
    //        [self.view addSubview:self.myView];
    //    }
}
#pragma mark -
#pragma mark - 创建View
-(void)creatView{
    UIView *upView = [[UIView alloc]initWithFrame:CGRectMake(0, 20, 320, 44)];
    upView.backgroundColor = [UIColor  colorWithRed:226/255.0 green:44/255.0 blue:56/255.0 alpha:1.0];
    [self.view addSubview:upView];
    [upView release];
}
#pragma mark -
#pragma mark - 创建table
-(void)creattable{
    self.table = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
    _table.delegate = self;
    _table.dataSource = self;
    [self.view addSubview:_table];
    
}
#pragma mark -
#pragma mark - 必须实现的协议
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [self.remarkArray count];
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    moreRemarkCell *more = [tableView dequeueReusableCellWithIdentifier:@"moreRemark"];
    remarkModel *remark = [self.remarkArray objectAtIndex:indexPath.row];
    
    [more.autherIcon setImageWithURL:[NSURL URLWithString:remark.authericon]];
    //半径设置
    more.autherIcon.layer.cornerRadius = 20;
    //变圆形属性
    more.autherIcon.clipsToBounds = YES;
    more.authername.text = remark.authername;
    more.time.text = remark.time;
    more.content.text = remark.content;
    
    return more;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    //    NSDictionary *dic = @{NSFontAttributeName :[UIFont systemFontOfSize:16]};
    //    CGRect rect = [[[self.remarkArray objectAtIndex:indexPath.row]objectForKey:@"content"] boundingRectWithSize:CGSizeMake(self.view.frame.size.width, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    //
    //    return rect.size.height + 75;
    return 150;
    
    
}
#pragma mark -
#pragma mark - 网络请求
-(void)connect{
    
    AFHTTPRequestOperationManager *manger = [AFHTTPRequestOperationManager manager];
    [manger.responseSerializer setAcceptableContentTypes: [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/css", nil]];
    if (self.i == 0) {
        self.str = self.recomend.pk;
    } else if (self.i == 1){
        self.str = self.pk;
    }
    NSString *str111 = @"http://c.myzaker.com/weibo/api_comment_article_url.php?_appid=iphone&_dev=iPhone%2C7.1.2&_v=4.4.4&_version=4.51&act=get_comments&pk=";
    //    str111 = [str111 stringByAppendingFormat:@"%@",str];
    //@"http://c.myzaker.com/weibo/api_comment_article_url.php?_appid=iphone&_dev=iPhone%2C7.1.2&_v=4.4.4&_version=4.51&act=get_comments&pk=5456eb189490cbb52f0000d3"
    
    [manger GET:[str111 stringByAppendingString:self.str]
     parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
         NSLog(@"%@",responseObject);
         self.myDic = responseObject;
         NSDictionary *dataDic = [self.myDic objectForKey:@"data"];
         NSArray *comments = [dataDic objectForKey:@"comments"];
         for (NSDictionary *myDic in comments) {
             NSArray *listArray = [myDic objectForKey:@"list"];
             for (NSDictionary *lastDic in listArray) {
                 remarkModel *remark = [[remarkModel alloc]initWithDic:lastDic];
                 [self.remarkArray addObject:remark];
                 [remark release];
                 
             }
         }
         [_table reloadData];
     } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
         NSLog(@"%@",error);
     }];
    
    
}
#pragma mark -
#pragma mark - 创建的button点击事件
-(void)buttonAction:(UIButton *)sender{
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
    
}
#pragma mark -
#pragma mark - 代理传值协议
-(void)sendArrayToWebView:(NSMutableArray *)array{
    
    [self.delegate sendArrayToWebView:(NSMutableArray *)[self.remarkArray count]];
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
 {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
